%% curveFitandResample
%
% Funtion to curve fit extended signal and downsample to orginal sampling
% frequency.
% 
%% Syntax
% [Time, FinalPressureValues] = curveFitandResample(Time_original,Time_Extended, PressureValues, NumberofResamplePoints)
%
% 
%
%
%% Description
% This function will take in the orignal time vector, the extended time
% vector, extended pressure values and number of points to resample the
% signal at and produce a smoothed resampled wave for input into FFT
% algorithum.
%
%% Arguments
%* Time_original                      - Double: A nx1 double of the orginal
%                                       time values of the pressure signal
%                                        
%
%* Time_Extended                      - Double: A nx1 double of the
%                                       extended time values of the pressure signal
%
%* PressureValues                     - Double: A nx1 double of the
%                                       extended pressure signal values
%
%* NumberofResamplePoints             - Double: A double precision number of
%                                        the number of points to resample at 
%
%* Period of Orignal Signal             - Double: A double precision number of
%                                        the original signal period 
%% Returns
%* Time                               - Double: A nx1 double precision vector of
%                                        the final time values 
%
%* FinalPressureValues                 - Double: A nx1 double precision vector
%                                        of smoothed final pressure data values
%                                        
%% Function Side Effects
%* N/A
%
%% Exceptions
%* An error is thrown if data type of input varaibles is not compatible
%  with what the function is expecting
%
%% See Also
% N/A

function [Time, FinalPressureValues] = curveFitandResample(Time_original, Time_Extended, PressureValues_Extended, NumberofResamplePoints, Tp)

if isa(Time_original, 'double') ~=1
    type = class(SamplingFrequency);
    error(sprintf(('Original Time Values is of type %s please change to type double'), type))    
end

if isa(Time_Extended, 'double') ~=1
    type = class(SamplingFrequency);
    error(sprintf(('Extended Time Values are of type %s please change to type double'), type))
end
    
if isa(PressureValues_Extended, 'double') ~=1
    type = class(SamplingFrequency);
    error(sprintf(('Extended Time Values are of type %s please change to type double'), type))
end

if isa(NumberofResamplePoints, 'double') ~=1
    type = class(SamplingFrequency);
    error(sprintf(('Number of resampling points are of type %s please change to type double'), type))    
end

if isa(Tp, 'double') ~=1
    type = class(SamplingFrequency);
    error(sprintf(('Number of resampling points are of type %s please change to type double'), type))    
end

%--- fit a curve to it and then resample ---%

p = fit(Time_Extended, PressureValues_Extended,'smoothingspline'); % Fitting of extented signal

%--- Create Fit over Middle Period---%  
n = find(Time_Extended == Tp);

size_n=size(n);
if size_n(1)>1 % This will ensure that n is a scalar (sometimes n is a vector)
    n=n(1);
end

theta=linspace(Time_Extended(n-1),Time_Extended(2*n),round(100*(NumberofResamplePoints+1)))'; % This is the time vector of the 2nd period of the signal
ydata1= feval(p,theta); % Here we evaluate the fit over the middle period to avoid leakage
%------------------------------------%

%--- Fit of 1st period---------%
theta0=linspace(0,Tp,round(100*(NumberofResamplePoints+1)))'; % This is a time vector of the 1st period of the signal
p=fit(theta0,ydata1,'smoothingspline'); % Refit curve over over this middle smoothed wave
%------------------------------%

%----Re-evaluates fit over the orignal time vector to get back resampled
%signal---------------------%

t = linspace(0,Tp,(NumberofResamplePoints+1))'; % Generate vector for time period of orginal wave
Time = t(1:end-1); % Remove last value as its left over from the repeating of the waves
FinalPressureValues = feval(p,Time); % Generate vector of the values to be outputed for FFT.
